$(function () {
    getProductTypeOption();
    getUserTypeOption();
    $("#name").change(function () {
        if ($("#name").val()!=""){
            $("#name_tips").html("");
            flag = true;
        }
    });
    $("#name").blur(function () {
        if ($("#name").val()==""){
            $("#name_tips").html("请输入产品名称");
            flag = false;
        }
    });
});
var flag = true;
function productadd() {
    let photo = sessionStorage.getItem("productphoto");
    console.log(photo);
    let data = {
        'name':$("#name").val(),
        'user_type':$("#user_type").find("option:selected").val(),
        'product_type':$("#product_type").find("option:selected").val(),
        'price':$("#price").val(),
        'photo':photo
    };
    if (photo!=""&&flag&&photo!=null&&$("#name").val()!=null&&$("#name").val()!=""){
        $.ajax({
            url:'/back/product/add',
            type:'post',
            data:data,
            dataType:'text',
            success:function (data) {
                console.log(data);
                if (data==1){
                    alert("添加成功！");
                    $(".right").load("../html/back/product/product.html");
                }else if (data==0){
                    alert("添加失败！");
                }
            },
            error:function (data) {
                alert(data);
            }
        });
    }else if (photo==""||photo==null)
        alert("请选择产品图片！");
    else
        alert("请完善产品添加信息！");
}

function add() {
    let formData = new FormData();
    formData.append('name',$("#name").val());
    formData.append('user_type',$("#user_type").find("option:selected").val());
    formData.append('product_type',$("#product_type").find("option:selected").val());
    formData.append('price',$("#price").val());
    formData.append('photo',$("#productphoto")[0].files[0]);
    $.ajax({
        url:'/back/product/add',
        type:'post',
        data:formData,
        dataType:'text',
        processData: false,
        contentType: false,
        success:function (data) {
            console.log(data);
            if (data==1){
                alert("添加成功！");
                $(".right").load("../html/back/product/product.html");
            }else if (data==0){
                alert("添加失败！");
            }
        },
        error:function (data) {
            alert(data);
        }
    })
}

function pictureChange() {
    let formData = new FormData();//获取表单中的文件
    let productphoto = $('#productphoto')[0].files[0];
    if (productphoto!=null && productphoto!=undefined){
        formData.append("photo",productphoto);
        $.ajax({
            url: "/upload",
            type: 'post',
            data: formData,
            dataType: 'text',
            processData: false,
            contentType: false,
            success: function (data) {
                console.log(data);
                if (data != "") {
                    $("#productimg").attr("src", data);
                    sessionStorage.setItem("productphoto", data);
                } else if (data == 0) {
                    console.log("图片上传失败！");
                }
            },
            error: function () {
                alert("图片上传异常");
            }
        })
    }
}

function getUserTypeOption() {
    $.ajax({
        url: '/back/product/usertype',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            $("#user_type").html("");
            $.each(data.userType,function (i,usertype) {
                $("#user_type").append("<option value='"+ usertype.id +"'>"+ usertype.name +"</option>");
            });
        }
    })
}

function getProductTypeOption() {
    $.ajax({
        url: '/back/product/type',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            $("#product_type").html("");
            $.each(data.productType,function (i,producttype) {
                $("#product_type").append("<option value='"+ producttype.id +"'>"+ producttype.name +"</option>");
            });
        }
    })
}